package com.piece.admin.thymeleaf;

import com.piece.admin.api.dto.SysUserDTO;
import com.piece.admin.model.TbSysConfig;
import com.piece.admin.service.TbSysConfigService;
import com.piece.core.framework.enums.OptionType;
import com.piece.core.framework.properties.ApplicationProperties;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.dto.UserDetailDTO;
import com.piece.core.framework.util.time.DateUtil;
import com.piece.core.oauth.social.AuthRequestFactory;
import com.piece.core.web.util.AuthenticationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * thymeleaf 实现参数管理
 */
@Service("config")
public class ConfigService {

    @Autowired
    private ApplicationProperties applicationProperties;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TbSysConfigService tbSysConfigService;

    @Autowired
    private AuthRequestFactory authRequestFactory;

    /**
     * 根据键名查询参数配置信息
     */
    public String getKey(String configKey) {
        TbSysConfig config = tbSysConfigService.findByConfigKey(configKey);
        return null == config ? "" : config.getConfigValue();
    }

    /**
     * 根据键名查询参数配置信息
     */
    public String getApplicationOption(String key) {
        return Convert.toStr(applicationProperties.getValue(key));
    }

    /**
     * 获取描述性时间
     */
    public String getTimeAgo(String time) {
        try {
            return DateUtil.getDescriptionTimeFromTimestamp(DateUtil.getDate(time, DateUtil.FORMAT_YYYY_MM_DD_HH_MM_SS).getTime());
        } catch (ParseException e) {
            return "";
        }
    }

    /**
     * 获取第三方登陆信息
     */
    public List<Map<String, String>> getOauth() {
        List<String> social_type = authRequestFactory.socialList();
        return social_type.stream().map(type -> {
            Map<String, String> social_map = new HashMap<>();
            switch (type) {
                case "WECHAT_ENTERPRISE":
                    social_map.put("type", "WECHAT_ENTERPRISE");
                    social_map.put("title", "企业微信");
                    social_map.put("image", "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.11/wechat_enterprise.png");
                    break;
                case "DINGTALK":
                    social_map.put("type", "DINGTALK");
                    social_map.put("title", "钉钉");
                    social_map.put("image", "https://cdn.jsdelivr.net/gh/justauth/justauth-oauth-logo@1.11/dingtalk.png");
                    break;
            }

            return social_map;
        }).collect(Collectors.toList());
    }

    /**
     * 获取年龄
     */
    public int getAge(Date date) throws ParseException {
        return DateUtil.dayDiff(date, new Date());
    }

    public String hasPermission(String permission) {
        return isPermittedOperator(permission) ? "" : "readonly";
    }

    public boolean isPermittedOperator(String permission) {
        return AuthenticationUtil.getInstance(request).getLoginUser().isPermitted(permission);
    }

    public String isFrontUser() {
        UserDetailDTO userDetail = AuthenticationUtil.getInstance(request).getLoginUser();
        SysUserDTO user = (SysUserDTO) userDetail.getDetail();
        return OptionType.GROUP.getCode().equals(user.getUserType()) ? "" : "readonly";
    }
}
